package com.jyw.business.ast.service.impl;

import java.util.List;

import com.jyw.business.ast.constant.AstRelationConstant;
import com.jyw.business.ast.domain.AstDealRelation;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.domain.dto.web.AstAllocateDto;
import com.jyw.business.ast.domain.dto.web.AstAllocatePageDto;
import com.jyw.business.ast.domain.vo.web.AllocateAstVo;
import com.jyw.business.ast.domain.vo.web.AllocateDetailVo;
import com.jyw.business.ast.domain.vo.web.AllocatePageVo;
import com.jyw.business.ast.service.IAstDealRelationService;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.DateUtils;
import com.jyw.system.utils.ConversionUtil;
import org.springframework.stereotype.Service;
import com.jyw.business.ast.mapper.AstAllocateMapper;
import com.jyw.business.ast.domain.AstAllocate;
import com.jyw.business.ast.service.IAstAllocateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 资产-调拨信息Service业务层处理
 * 
 * @author system
 * @date 2023-10-19
 */
@Service
public class AstAllocateServiceImpl extends ServiceImpl<AstAllocateMapper,AstAllocate> implements IAstAllocateService
{
    @Resource
    private AstAllocateMapper astAllocateMapper;

    @Resource
    private IAstDealRelationService relationService;

    @Resource
    private IAstInfoService infoService;

    /**
     * 查询资产-调拨信息
     * 
     * @param id 资产-调拨信息主键
     * @return 资产-调拨信息
     */
    @Override
    public AstAllocate selectAstAllocateById(Long id){
        return astAllocateMapper.selectAstAllocateById(id);
    }

    /**
     * 查询资产-调拨信息列表
     * 
     * @param astAllocate 资产-调拨信息
     * @return 资产-调拨信息
     */
    @Override
    public List<AstAllocate> selectAstAllocateList(AstAllocate astAllocate)
    {
        return astAllocateMapper.selectAstAllocateList(astAllocate);
    }

    /**
     * 新增资产-调拨信息
     * 
     * @param astAllocate 资产-调拨信息
     * @return 结果
     */
    @Override
    public int insertAstAllocate(AstAllocate astAllocate)
    {
        astAllocate.setCreateTime(DateUtils.getNowDate());
        return astAllocateMapper.insertAstAllocate(astAllocate);
    }

    /**
     * 修改资产-调拨信息
     * 
     * @param astAllocate 资产-调拨信息
     * @return 结果
     */
    @Override
    public int updateAstAllocate(AstAllocate astAllocate)
    {
        return astAllocateMapper.updateAstAllocate(astAllocate);
    }

    /**
     * 批量删除资产-调拨信息
     * 
     * @param ids 需要删除的资产-调拨信息主键
     * @return 结果
     */
    @Override
    public int deleteAstAllocateByIds(Long[] ids)
    {
        return astAllocateMapper.deleteAstAllocateByIds(ids);
    }

    /**
     * 删除资产-调拨信息信息
     * 
     * @param id 资产-调拨信息主键
     * @return 结果
     */
    @Override
    public int deleteAstAllocateById(Long id)
    {
        return astAllocateMapper.deleteAstAllocateById(id);
    }

    @Override
    public List<AllocatePageVo> allocatePage(AstAllocatePageDto dto) {
        //将结束日期设置为yyyy-MM-dd 23:59:59
        dto.setCreateEndTime(DateUtils.toDateLastTime(dto.getCreateEndTime()));
        List<AllocatePageVo> list = astAllocateMapper.allocatePage(dto);
        return ConversionUtil.conversionList(list,AllocatePageVo.class);
    }

    /**
     * 资产调拨
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void allocate(AstAllocateDto dto) {
        AstAllocate allocate = dto.buildAllocate();
        //保存资产调拨信息
        save(allocate);
        //封装调拨关系记录
        List<AstDealRelation> list = dto.buildRelation(allocate);
        relationService.saveBatch(list);
        //批量修改使用科室
        List<AstInfo> infoList = dto.buildAstInfo();
        infoService.updateBatchById(infoList);
    }

    @Override
    public AllocateDetailVo selectDetail(Long id) {
        AstAllocate allocate = getById(id);
        //查询调拨详情
        AllocateDetailVo detail = new AllocateDetailVo(allocate);
        List<AllocateAstVo> list = relationService.allocateRelation(allocate.getId());
        detail.setList(list);
        detail = ConversionUtil.conversion(detail, AllocateDetailVo.class);
        return detail;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long id) {
        //判断该调拨记录中的其他资产是否发生了新的调拨,转移,借用
        if (!relationService.canRevoke(new AstDealRelation(){{setBusinessId(id);setType(AstRelationConstant.ALLOCATE);}})) {
            throw new ServiceException("此调拨中有资产产生了二次调拨，转移，借用，不可撤销");
        }
        //查询调拨记录
        //删除该调拨记录下的关系记录
        relationService.lambdaQuery().eq(AstDealRelation::getType,AstRelationConstant.ALLOCATE).eq(AstDealRelation::getBusinessId,id).list().forEach(data->{
            //还原使用科室
            infoService.updateById(new AstInfo(){{setId(data.getAstId());setUseDept(data.getOutDept());}});
            //删除该关系记录
            relationService.removeById(data.getId());
        });
        //删除调拨记录
        removeById(id);
    }
}
